package com.xinbochuang.machine.admin.service.impl.plan;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinbochuang.machine.admin.domain.plan.CheckPlanRoom;
import com.xinbochuang.machine.admin.domain.vo.MacRoomVo;
import com.xinbochuang.machine.admin.mapper.plan.CheckPlanRoomMapper;
import com.xinbochuang.machine.admin.mapper.plan.MacRoomVoMapper;
import com.xinbochuang.machine.admin.service.plan.ICheckPlanRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 黄晓鹏
 * @date 2020-09-10
 */
@Service
public class CheckPlanRoomServiceImpl extends ServiceImpl<CheckPlanRoomMapper, CheckPlanRoom> implements ICheckPlanRoomService {

    @Resource
    private MacRoomVoMapper roomVoMapper;

    @Override
    public TableDataInfo selectPlanRoomListByApp(CheckPlanRoom checkPlanRoom) {
        //根据查询条件查询集合列表
        QueryWrapper<CheckPlanRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CheckPlanRoom::getPlanId,checkPlanRoom.getPlanId());
        queryWrapper.lambda().orderByAsc(CheckPlanRoom::getStatus);
        List<CheckPlanRoom> planList;
        IPage<CheckPlanRoom> list = new Page<>();
        if (checkPlanRoom.getPageNum() == null) {
            planList = list(queryWrapper);
            list.setRecords(planList);
            list.setTotal(planList.size());
        } else {
            list = page(new Page<>(checkPlanRoom.getPageNum(), checkPlanRoom.getPageSize()), queryWrapper);
            planList = list.getRecords();
        }
        List<String> romeIds = new ArrayList<>();
        Map<String,String> roomMap=new HashMap<>();
        for (CheckPlanRoom planRome : planList) {
            romeIds.add(planRome.getRoomId());
        }
        List<MacRoomVo> roomList = roomVoMapper.selectList(new QueryWrapper<MacRoomVo>().in("uuid", romeIds));

        for (MacRoomVo macRoomVo : roomList) {
            roomMap.put(macRoomVo.getUuid(),macRoomVo.getName());
        }

        for(CheckPlanRoom checkPlanRoom1:planList){
            if(roomMap.containsKey(checkPlanRoom1.getRoomId())){
                checkPlanRoom1.setName(roomMap.get(checkPlanRoom1.getRoomId()));
            }
        }
        /**
         * 根据计划/机房对应，查询机房列表，放入计划中
         */
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.HTTP_OK);
        rspData.setMsg("查询成功");
        rspData.setRows(list.getRecords());
        rspData.setTotal(list.getTotal());
        return rspData;
    }
}
